<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电子书管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #2c3e50;
            --secondary-color: #6c757d;
            --accent-color: #3498db;
            --sidebar-bg: #f8f9fa;
        }

        body {
            background-color: #f8f9fa;
            font-family: 'Helvetica Neue', Helvetica, Arial, 'PingFang SC', 'Microsoft YaHei', sans-serif;
            padding: 2rem 0;
        }

        .container-wrapper {
            max-width: 1200px;
            margin: 0 auto;
        }

        /* 搜索框功能 */
        .search-header {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            margin-bottom: 3rem;
            box-shadow: 0 4px 20px rgba(0,0,0,0.05);
        }

        .search-label {
            font-size: 1.4rem;
            color: var(--primary-color);
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-bottom: 1rem;
        }

        .search-input {
            border: 2px solid var(--accent-color);
            border-radius: 8px;
            padding: 1rem 1.5rem;
            font-size: 1.1rem;
            transition: all 0.3s ease;
            width: 100%;
        }

        .search-input:focus {
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
            outline: none;
        }

        /* 分类侧边栏样式 */
        .sidebar {
            position: fixed;
            top: 80px;
            left: 0;
            bottom: 0;
            width: 300px;
            padding: 20px;
            background-color: var(--sidebar-bg);
            color: #000;
            overflow-y: auto;
            border-right: 1px solid #dee2e6;
            z-index: 1000;
            transition: all 0.3s ease;
        }

        .category-title {
            font-size: 1.2rem;
            font-weight: 600;
            margin-bottom: 15px;
            color: var(--primary-color);
        }

        .all-books {
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }

        .all-books a {
            color: var(--accent-color);
            text-decoration: none;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .all-books a:hover {
            text-decoration: underline;
        }

        .category-card {
            background-color: white;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 10px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .category-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }

        .category-card.active {
            background-color: var(--accent-color);
            color: white;
        }

        .category-name {
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .category-count {
            background-color: var(--secondary-color);
            color: white;
            font-size: 0.8rem;
            padding: 2px 8px;
            border-radius: 10px;
            margin-left: auto;
        }

        /* 电子书卡片样式 */
        .ebook-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
            margin-bottom: 2rem;
            padding: 1.5rem;
            transition: transform 0.3s ease;
        }

        .ebook-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 15px rgba(0,0,0,0.1);
        }

        .ebook-title {
            font-size: 1.5rem;
            color: var(--primary-color);
            margin-bottom: 0.5rem;
        }

        .ebook-author {
            font-size: 1.1rem;
            color: var(--secondary-color);
            margin-bottom: 1rem;
        }

        .meta-info {
            color: #666;
            margin: 1.5rem 0;
            display: grid;
            gap: 1rem;
        }

        .meta-item {
            display: flex;
            align-items: center;
            gap: 0.8rem;
            font-size: 16px;
        }

        .meta-title {
            color: var(--accent-color);
            font-weight: 600;
        }

        .tag-container {
            display: flex;
            flex-wrap: wrap;
            gap: 0.8rem;
            margin: 1.5rem 0;
        }

        .tag-badge {
            background: #e9ecef;
            padding: 0.4rem 1rem;
            border-radius: 20px;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            font-size: 0.9rem;
        }

        .action-buttons {
            border-top: 1px solid #eee;
            padding-top: 1.5rem;
            margin-top: 1.5rem;
        }

        .btn-primary {
            background-color: var(--accent-color);
            border: none;
        }

        .btn-primary:hover {
            background-color: #2980b9;
        }

        /* 主内容区域 */
        .content {
            margin-left: 320px;
            padding: 0 2rem;
        }

        /* 响应式调整 */
        @media (max-width: 992px) {
            .sidebar {
                width: 250px;
            }
            .content {
                margin-left: 270px;
            }
        }

        @media (max-width: 768px) {
            .sidebar {
                display: none;
            }
            .content {
                margin-left: 0;
            }
        }

        /* 返回顶部按钮 */
        .back-to-top {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: var(--accent-color);
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            opacity: 0;
            transition: opacity 0.3s ease;
            z-index: 1000;
        }

        .back-to-top.show {
            opacity: 1;
        }

        .back-to-top:hover {
            background-color: #2980b9;
        }
    </style>
</head>
<body>
    <div class="container-wrapper">
        <!-- 分类侧边栏 -->
        <div class="sidebar">
            <div class="category-title">
                <i class="fas fa-book"></i>
                电子书分类
            </div>
            
            <!-- 所有书籍选项 -->
            <div class="all-books">
                <a href="javascript:void(0)" onclick="showAllBooks()">
                    <i class="fas fa-th-list"></i>
                    所有电子书
                </a>
            </div>

            <div id="categoryList">
                <!-- 分类卡片将通过JavaScript动态生成 -->
            </div>
        </div>

        <!-- 主内容区域 -->
        <div class="content">
            <!-- 搜索框功能 -->
            <div class="search-header">
                <div class="search-label">
                    <i class="fas fa-search"></i>
                    电子书检索
                </div>
                <input type="text" 
                       id="searchInput"
                       class="search-input"
                       placeholder="输入书名、作者、出版社或标签进行搜索...">
            </div>

            <!-- 电子书列表 -->
            <div id="ebookList">
                <!-- 电子书卡片将通过JavaScript动态生成 -->
            </div>
        </div>
    </div>

    <!-- 返回顶部按钮 -->
    <div class="back-to-top" id="backToTop">
        <i class="fas fa-arrow-up"></i>
    </div>

    <!-- 引入Bootstrap和FontAwesome -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 从Jinja2传递的电子书数据
        const allEbooks = {{ ebooks | tojson }};
        let currentEbooks = [...allEbooks];

        // 创建电子书卡片
        function createEbookCard(ebook) {
            const card = document.createElement('div');
            card.className = 'ebook-card';
            card.dataset.search = `${ebook.name.toLowerCase()} ${ebook.author.toLowerCase()} ${ebook.publisher.toLowerCase()} ${ebook.tags.join(' ').toLowerCase()}`;
            card.innerHTML = `
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h3 class="ebook-title">${ebook.name}</h3>
                    <div class="text-muted">${ebook.category}</div>
                </div>
                
                <div class="ebook-author">
                    ${ebook.author}
                </div>

                <div class="meta-info">
                    <div class="meta-item">
                        <span class="meta-title">出版社:</span>
                        <span>${ebook.publisher}</span>
                        <span style="margin-left: 15px;"><span class="meta-title">出版年份:</span> ${ebook.publish_year}</span>
                    </div>
                </div>

                ${ebook.tags.length > 0 ? `
                    <div class="tag-container">
                        ${ebook.tags.map(tag => `
                            <span class="tag-badge">
                                ${tag}
                            </span>
                        `).join('')}
                    </div>
                ` : ''}
                
                <div class="action-buttons">
                    <a href="javascript:void(0)" 
                       class="btn btn-primary open-ebook"
                       data-file="${ebook.file_path}">
                        <i class="fas fa-book-open"></i>
                        打开电子书
                    </a>
                </div>
            `;
            return card;
        }

        // 搜索功能
        document.getElementById('searchInput').addEventListener('input', function(e) {
            const searchTerm = e.target.value.toLowerCase().trim();
            const ebookList = document.getElementById('ebookList');
            ebookList.innerHTML = '';
            
            if (!searchTerm) {
                // 如果没有搜索词，显示当前分类下的电子书
                if (currentEbooks.length === 0) {
                    ebookList.innerHTML = '<div class="alert alert-info">没有电子书</div>';
                    return;
                }
                
                currentEbooks.forEach(ebook => {
                    const card = createEbookCard(ebook);
                    ebookList.appendChild(card);
                });
            } else {
                // 如果有搜索词，全局搜索所有电子书
                const searchResults = allEbooks.filter(ebook => 
                    ebook.name.toLowerCase().includes(searchTerm) ||
                    ebook.author.toLowerCase().includes(searchTerm) ||
                    ebook.publisher.toLowerCase().includes(searchTerm) ||
                    ebook.tags.some(tag => tag.toLowerCase().includes(searchTerm))
                );
                
                if (searchResults.length === 0) {
                    ebookList.innerHTML = '<div class="alert alert-info">没有找到匹配的电子书</div>';
                } else {
                    searchResults.forEach(ebook => {
                        const card = createEbookCard(ebook);
                        ebookList.appendChild(card);
                    });
                }
            }
            
            // 为新添加的按钮绑定事件
            document.querySelectorAll('.open-ebook').forEach(button => {
                button.addEventListener('click', function() {
                    const filePath = this.getAttribute('data-file');
                    window.open(filePath, '_blank');
                });
            });
        });

        // 构建分类列表
        function buildCategoryList() {
            const categoryListElement = document.getElementById('categoryList');
            categoryListElement.innerHTML = '';
            
            // 获取所有唯一的分类
            const categories = [...new Set(allEbooks.map(ebook => ebook.category))];
            
            // 按字母顺序排序
            categories.sort();
            
            // 渲染分类卡片
            categories.forEach(category => {
                const categoryCount = allEbooks.filter(ebook => ebook.category === category).length;
                const categoryCard = document.createElement('div');
                categoryCard.className = 'category-card';
                categoryCard.innerHTML = `
                    <div class="category-name">
                        <i class="fas fa-folder me-2"></i>
                        ${category}
                        <span class="category-count">${categoryCount}</span>
                    </div>
                `;
                categoryCard.addEventListener('click', function() {
                    filterEbooksByCategory(category);
                    
                    // 更新活跃状态
                    document.querySelectorAll('.category-card').forEach(card => {
                        card.classList.remove('active');
                    });
                    this.classList.add('active');
                });
                
                categoryListElement.appendChild(categoryCard);
            });
        }

        // 按分类筛选电子书
        function filterEbooksByCategory(category) {
            currentEbooks = allEbooks.filter(ebook => ebook.category === category);
            
            const ebookList = document.getElementById('ebookList');
            ebookList.innerHTML = '';
            
            if (currentEbooks.length === 0) {
                ebookList.innerHTML = '<div class="alert alert-info">没有找到匹配的电子书</div>';
                return;
            }
            
            currentEbooks.forEach(ebook => {
                const card = createEbookCard(ebook);
                ebookList.appendChild(card);
            });
            
            // 为新添加的按钮绑定事件
            document.querySelectorAll('.open-ebook').forEach(button => {
                button.addEventListener('click', function() {
                    const filePath = this.getAttribute('data-file');
                    window.open(filePath, '_blank');
                });
            });
        }

        // 显示所有电子书
        function showAllBooks() {
            currentEbooks = [...allEbooks];
            
            const ebookList = document.getElementById('ebookList');
            ebookList.innerHTML = '';
            
            if (currentEbooks.length === 0) {
                ebookList.innerHTML = '<div class="alert alert-info">没有电子书</div>';
                return;
            }
            
            currentEbooks.forEach(ebook => {
                const card = createEbookCard(ebook);
                ebookList.appendChild(card);
            });
            
            // 为新添加的按钮绑定事件
            document.querySelectorAll('.open-ebook').forEach(button => {
                button.addEventListener('click', function() {
                    const filePath = this.getAttribute('data-file');
                    window.open(filePath, '_blank');
                });
            });
            
            // 重置分类卡片的活跃状态
            document.querySelectorAll('.category-card').forEach(card => {
                card.classList.remove('active');
            });
        }

        // 返回顶部功能
        const backToTopButton = document.getElementById('backToTop');
        
        window.addEventListener('scroll', function() {
            if (window.pageYOffset > 300) {
                backToTopButton.classList.add('show');
            } else {
                backToTopButton.classList.remove('show');
            }
        });

        backToTopButton.addEventListener('click', function() {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });

        // 初始化
        window.onload = function() {
            buildCategoryList();
            showAllBooks();
        };
    </script>
</body>
</html>